<?php

declare(strict_types=1);

namespace app\model;

use app\model\Base;
use think\facade\Db;

/**
 * 菜单(权限)模型
 */
class SysMenu extends Base
{
    protected $pk = 'menuid';

    protected $schema = [
        "menuid" => "int",  // 
        "pid" => "int",  // 父id
        "name" => "varchar",  // 菜单名
        "url" => "varchar",  // 菜单URL
        "type" => "tinyint",  // 类型 0：目录 1：菜单 2：按钮
        "method" => "tinyint",  // method: 0, get, 1, post, 2 rule(get and post)
        "icon" => "varchar",  // 图标
        "sort" => "smallint",  // 排序
        "open" => "tinyint",  // 是否公用权限不限制, 0 否, 1, 是
    ];

    protected $autoWriteTimestamp = false;

    public static function getUserMenuList($rid)
    {
        if ($rid === 1) {   // 超级管理员
            $data = self::field('menuid, pid, name, url, type, icon')->select();
        } else {
            $roleModel = new SysRole();
            $permission_ids = $roleModel->getpermissionIds($rid);
            $data = self::where('menuid', 'IN', $permission_ids)->field('menuid, pid, name, url, icon')->select();
        }
        return list_tree($data, 'menuid');
    }

    public static function queryButtonPermissionsByRoleid($rid)
    {
        if ($rid === 1) {   // 超级管理员
            $data = self::where('type', 2)->column('url');
        } else {
            $roleModel = new SysRole();
            $permission_ids = $roleModel->getpermissionIds($rid);
            $data = self::where('menuid', 'IN', $permission_ids)->where('type', 2)->column('url');
        }

        return $data;
    }

    /**
     * 面包屑
     */
    public static function getBreadCrumb($route)
    {
        $active =  \app\model\SysMenu::where('url', $route)->find();

        $active_pid = 0;

        $breadCrumb = [];

        if ($active) {
            $active_pid = $active->pid;

            $result = \app\model\SysMenu::find($active_pid);
            // 如果还有上级
            if ($result) {
                $res = \app\model\SysMenu::find($result->pid);
                // 如果还有上级
                if ($res) {
                    $active_pid = $result->pid;
                    $url = $res['url'] ? '/sys/' . $res['url'] : null;
                    $breadCrumb[] = [
                        'url'   => $url,
                        'title' => $res['name'],
                    ];
                }
                $url = $result['url'] ? '/sys/' . $result['url'] : null;
                $breadCrumb[] = [
                    'url'   => $url,
                    'title' => $result['name'],
                ];
            }
            $breadCrumb[] = [
                'url'   => null,
                'title' => $active['name'],
            ];
        } else {
            $breadCrumb[] = [
                'url'   => null,
                'title' => '我的桌面',
            ];
        }

        return [
            'active_pid' => $active_pid,
            'breadCrumb' => $breadCrumb
        ];
    }

    /**
     * 后台首页快捷方式
     */
    public static function getIndexButton()
    {
        return self::where('method', 1)->where('type', 1)->select();
    }

    public static function getMenuList()
    {
        return self::where('type', 1)->select();
    }
}
